﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PERMISSION.Base.Objects;
using ERP_RORZE_PERMISSION.UserControls;

namespace ERP_RORZE_PERMISSION.Forms
{
    public partial class frm_ChangePass : DevExpress.XtraEditors.XtraForm
    {
        private Permission role;
        private LogUtils log;
        public frm_ChangePass()
        {
            InitializeComponent();
            role = new Permission();
            this.ActiveControl = txtCurrent;
            log = new LogUtils();
        }

        private void frm_UserAdd_Load(object sender, EventArgs e)
        {
            if (role.Is_Login())
            {
                txtUserName.Text = role.UserName;
            }
            else
            {
                Utilities.MessageErrorOk(PermissionConstants.Message.LoadFailed);
                this.Dispose();
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            Users user = new Users();
            if (GetData(user))
            {
                string insert = "UPDATE [222_Users] " +
                                "SET [Password]= N'" + user.Password + "'" +
                                " where [UserName]=N'" + user.UserName + "'";
                if (BaseDAO.ExecuteNonQuery(insert) > 0)
                {
                    //write Log
                    log.Table = LogConstant.Table.SystemAdmin.Users;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "Change password: UserName: " + user.UserName + "; Password: " + user.Password;
                    log.WriteLog();

                    Utilities.MessageOk(PermissionConstants.Message.UpdateSuccess);
                    string text = Utilities.Encrypt(PermissionConstants.PasswordHash, PermissionConstants.SaltKey, PermissionConstants.VIKey, user.UserName) + "\n" + Utilities.Encrypt(PermissionConstants.PasswordHash, PermissionConstants.SaltKey, PermissionConstants.VIKey, user.Password);
                    Utilities.WriteToFile(PermissionConstants.FileName, text);
                    this.Dispose();
                }
                else
                {
                    Utilities.MessageErrorOk(PermissionConstants.Message.UpdateFailed);
                }
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        #region Private methods

        private bool GetData(Users o)
        {
            #region Check valid
            if (Utilities.IsNullOrEmpty(txtCurrent.Text.Trim()))
            {
                MessageBox.Show("Current password can not be empty.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCurrent.Focus();
                return false;
            }

            if (!Permission.CheckLogin(txtUserName.Text, txtCurrent.Text))
            {
                MessageBox.Show("Your current password is incorrect.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCurrent.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtNew.Text.Trim()))
            {
                MessageBox.Show("New pasword can not be empty.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtNew.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtRetypeNew.Text.Trim()))
            {
                MessageBox.Show("Please re-type your new password.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtRetypeNew.Focus();
                return false;
            }

            if (!txtRetypeNew.Text.Trim().Equals(txtNew.Text.Trim()))
            {
                MessageBox.Show("You must enter the same password twice in order to confirm it.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtRetypeNew.Focus();
                return false;
            }

            #endregion

            o.UserName = txtUserName.Text;
            o.Password = Utilities.EncryptPassword(txtNew.Text, PermissionConstants.Key);

            return true;
        }


        #endregion

    }
}